package com.tang.util;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * CreateTime: 2016-04-13上午11:03
 * Author: wjkjinke00@126.com
 * Description:
 */
public class StringUtil {

    static final String passwordRegex = "[A-Za-z0-9]{8,20}";

    public static <T> boolean isEmpty(T t) {
        String str;
        if (t instanceof TextView) {
            str = ((TextView) t).getText().toString();
        } else if (t instanceof String) {
            str = (String) t;
        } else {
            return true;
        }

        if (str.trim().equals("") || str.equals("null")) {
            return true;
        }
        return false;
    }

    public static String[] splite(String str) {
        if (isEmpty(str)) {
            return null;
        }
        String[] split = str.split(",");
        return split;
    }

    public static String getValue(String str) {
        if (null == str || str.trim().equals("") || str.equals("null")) {
            return "";
        }
        return str;
    }

    public static boolean isPasswordStrong(String password) {
        return password.matches(passwordRegex);
    }


    /**
     * textview设置其中某几个字体的颜色
     *
     * @param msg   内容
     * @param color 颜色
     * @param start 开的位置
     * @param end   结束的位置
     * @return
     */
    public static SpannableString setSpanStrColor(CharSequence msg, int color, int start, int end) {

        SpannableString spannableString = new SpannableString(msg);
        spannableString.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * textview设置其中某几个字体的大小
     *
     * @param msg   内容
     * @param size  大小
     * @param start 开始的位置
     * @param end   结束的位置
     * @return
     */
    public static SpannableString setSpanStrSize(CharSequence msg, int size, int start, int end) {
        SpannableString spannableString = new SpannableString(msg);
        spannableString.setSpan(new AbsoluteSizeSpan(size), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 字段去除空格
     *
     * @return
     */
    public static String SetString(String str) {
        str = str.trim();
        str = str.replaceAll(" ", "");
        str = str.replaceAll(" +", "");
        str = str.replaceAll(" *", "");//正则表达式  *为0到无穷
        str = str.replaceAll("\\s*", "");
        return str;
    }

    /**
     * 验证邮箱
     *
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 去掉手机号内除数字外的所有字符
     *
     * @param phoneNum 手机号
     * @return
     */
    public static String formatPhoneNum(String phoneNum) {
        String regex = "(\\+86)|[^0-9]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phoneNum);
        return matcher.replaceAll("");
    }

}
